package org.hp.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hp.bean.Admin;
import org.hp.bean.Patient;
import org.hp.service.admin.AdminService;
import org.hp.util.Layui;
import org.hp.util.Random;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Controller
public class AdminController {
    Layui layui=new Layui();
    @Resource
    AdminService adminService;

    @RequestMapping("/adminquery")
    public void adminquery(Admin admin,Integer page, Integer limit, HttpServletRequest request, HttpServletResponse response) throws IOException {
        PageHelper.startPage(page,limit);
        List<Admin> list=adminService.adminquery(admin);
        PageInfo pageInfo=new PageInfo(list,limit);
        response.getWriter().write(JSON.toJSONString(layui.select(pageInfo)));
    }

    @RequestMapping("/adminquery2")
    public void adminquery2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> list=adminService.adminquery2();
        response.getWriter().write(JSON.toJSONString(layui.select(list.size(),list)));
    }

    @RequestMapping("/adminqueryone")
    public void adminqueryone(Integer aid,HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> list=adminService.adminqueryone(aid);
        response.getWriter().write(JSON.toJSONString(layui.select(list.size(),list)));
    }

    @RequestMapping("/adminupdate")
    public void adminupdate(Admin admin,HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (adminService.adminupdate(admin)) {
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        } else {
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    @RequestMapping("/adminupdate2")
    public void adminupdate2(Admin admin,HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (adminService.adminupdate2(admin)) {
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        } else {
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    @RequestMapping("/adminupdate3")
    public void adminupdate3(Admin admin,HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (adminService.adminupdate3(admin)) {
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        } else {
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    @RequestMapping("/admininsert")
    public void admininsert(Admin admin,HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        String auserId = Random.GetAUserId();
        admin.setAuserId(auserId);
        if (adminService.admininsert(admin)) {
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        } else {
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

}
